package org.mosquitos.chat;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import net.jxta.exception.PeerGroupException;
import net.jxta.endpoint.EndpointAddress;
import net.jxta.peergroup.PeerGroup;
import net.jxta.platform.NetworkManager;

import org.mosquitos.chat.util.UserUtil;

public class JTalk {
	private Map<String,Object> userList = new HashMap<String,Object>();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		JTalk t = new JTalk();
		try {
			t.init(args[0]);
		} catch (PeerGroupException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public void init(String name) throws IOException, PeerGroupException{
		System.setProperty("net.jxta.logging.Logging", "OFF");
		System.setProperty("net.jxta.level", "OFF"); 
		if(name == null){
			name=UserUtil.getRandomUsername();
		}
		
        NetworkManager manager = new NetworkManager(NetworkManager.ConfigMode.ADHOC, name,new File(new File(".cache"),name).toURI());
        manager.startNetwork();
        
        PeerGroup pg = manager.getNetPeerGroup();
        
        Thread eads = new  Thread(new EndpointAddressDiscoveryService(pg,userList));
        eads.start();
        
        Thread rs = new Thread(new ReceiveService(pg));
        rs.start();
        
        
        Thread ss = new Thread(new SendService(pg.getEndpointService(),userList,name));
        ss.start();
        
        //manager.stopNetwork();
	}

}
